package com.health.mapp.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.alibaba.fastjson.JSONObject;
import com.health.db.model.HospitalSpecimen;
import com.health.db.model.HospitalSpecimenDetailExample;
import com.health.db.model.HospitalSpecimenExample;
import com.health.db.service.msg.IHospitalSpecimenService;

public class QueryHospitalSpecimen extends HttpServlet {

    private static final long serialVersionUID = -5955280799823737442L;

    public void init(ServletConfig config) throws ServletException {
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
                config.getServletContext());
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ServletContext servletContext = request.getSession().getServletContext();
        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        IHospitalSpecimenService hospitalSpecimenService = (IHospitalSpecimenService) applicationContext.getBean("hospitalSpecimenService");
        JSONObject object = null;
        //rucan
        String medicalCardNo = request.getParameter("medicalCardNo");
        String specimenId = request.getParameter("specimenId");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");

        List<HospitalSpecimen> hospitalSpecimenList;

        object = new JSONObject();
        HospitalSpecimenExample hospitalSpecimenExample = new HospitalSpecimenExample();
        //医生编号 用户编号 订单号 取一
        if (StringUtils.isNotBlank(medicalCardNo)) {
            if (medicalCardNo.contains(",")) {

                List<String> medicalCardNos = new ArrayList<String>();
                String[] orders = medicalCardNo.split(",");
                for (int i = 0; i < orders.length; i++) {
                    medicalCardNos.add(orders[i]);
                }
                hospitalSpecimenExample.setMedicalCardNos(medicalCardNos);
            } else {
                hospitalSpecimenExample.setMedicalCardNo(medicalCardNo);
            }
            object.put("errorCode", 0);
            object.put("hospitalSpecimenList", hospitalSpecimenService.selectByExample(hospitalSpecimenExample));

        } else if (StringUtils.isNotBlank(specimenId)) {

            hospitalSpecimenExample.setSpecimenId(specimenId);
            hospitalSpecimenList = hospitalSpecimenService.selectByExample(hospitalSpecimenExample);

            if (hospitalSpecimenList.size() == 1) {

                HospitalSpecimen hospitalSpecimenRep = hospitalSpecimenList.get(0);

                HospitalSpecimenDetailExample exampleDetail = new HospitalSpecimenDetailExample();
                exampleDetail.setSpecimenId(specimenId);

                hospitalSpecimenRep.setDetailList(hospitalSpecimenService.selectByExample(exampleDetail));
                object.put("hospitalSpecimen", hospitalSpecimenRep);
            }
            object.put("errorCode", 0);
        } else {
            object.put("errorCode", -1);
            object.put("errorMsg", "暂不支持!");
        }
        String callbackName = request.getParameter("jsonpCallback");// 客户端请求参数
        String jsonStr = object.toJSONString();
        String renderStr = callbackName+"("+jsonStr+")";
        response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,PUT,DELETE");
        response.setHeader("Access-Control-Allow-Headers", "cache-control,content-type,hash-referer,x-requested-with");
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.print(renderStr);
        out.println("\r\n");
        out.flush();
        out.close();
    }


    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        super.destroy();
    }

    @Override
    public void init() throws ServletException {
        // TODO Auto-generated method stub
        super.init();
    }

}
